<template>
  <el-cascader
    v-model="selectedValue"
    placeholder="请选择"
    :props="defaultProp"
    :options="addressJson"
    clearable
    filterable
    @change="handleChange"
  >
  </el-cascader>
</template>

<script>
import { regionData } from 'element-china-area-data'

export default {
  name: 'AddressSelector',
  data() {
    return {
      selectedValue: [],
      addressJson: regionData,
      defaultProp: {
        label: 'label',
        value: 'label',
        children: 'children'
      }
    }
  },
  created() {
    this.calcValue()
  },
  watch: {
    value() {
      this.calcValue()
    }
  },
  methods: {
    calcValue() {
      this.selectedValue = this.value ? this.value.split(' ') : []
    },
    handleChange() {
      this.$emit('input', this.selectedValue.join(' '))
    }
  },
  props: {
    value: {
      type: String,
      default: ''
    }
  }
}
</script>

